package com.project.openapi.controller;

import com.project.base.Result;
import com.project.dao.UserDao;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.support.BasicAuthenticationInterceptor;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class OpenApiController {

    @Resource
    private UserDao userDao;

    @Resource
    private RestTemplate restTemplate;

    @Resource
    private BCryptPasswordEncoder passwordEncoder;

    private String oauth2 = "http://127.0.0.1:8081/oauth/token";

    @RequestMapping("/token")
    public Result token(@RequestParam String username, @RequestParam String password, @RequestParam String clientID) {
        MultiValueMap<String, Object> paramsMap = new LinkedMultiValueMap<>(3);
        paramsMap.set("username", username);
        paramsMap.set("password", password);
        paramsMap.set("grant_type", "password");
        restTemplate.getInterceptors().clear();
        restTemplate.getInterceptors().add(new BasicAuthenticationInterceptor(clientID, "123"));
        Map token = restTemplate.postForObject(oauth2, paramsMap, Map.class);
        return Result.success(token);
    }

}
